/*
Copyright [2020] [https://www.xiaonuo.vip]

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

XiaoNuo采用APACHE LICENSE 2.0开源协议，您在使用过程中，需要注意以下几点：

1.请不要删除和修改根目录下的LICENSE文件。
2.请不要删除和修改XiaoNuo源码头部的版权声明。
3.请保留源码和相关描述文件的项目出处，作者声明等。
4.分发源码时候，请注明软件出处 https://gitee.com/xiaonuobase/xiaonuo-vue
5.在修改包名，模块名称，项目代码等时，请注明软件出处 https://gitee.com/xiaonuobase/xiaonuo-vue
6.若您的项目无法满足以上几点，可申请商业授权，获取XiaoNuo商业授权许可，请在官网购买授权，地址为 https://www.xiaonuo.vip
 */
package com.cn.xiaonuo.slbfinances.attractinfo.service.Impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cn.xiaonuo.common.entity.BusinessMessage;
import com.cn.xiaonuo.common.utils.DateUtils;
import com.cn.xiaonuo.common.utils.ExportExcelUtils;
import com.cn.xiaonuo.common.vo.ExcelData;
import com.cn.xiaonuo.core.exception.ServiceException;
import com.cn.xiaonuo.core.factory.PageFactory;
import com.cn.xiaonuo.core.pojo.page.PageResult;
import com.cn.xiaonuo.dbs.core.annotation.DataSource;
import com.cn.xiaonuo.slbfinances.attractinfo.entity.SlAttractUser;
import com.cn.xiaonuo.slbfinances.attractinfo.enums.SlAttractUserExceptionEnum;
import com.cn.xiaonuo.slbfinances.attractinfo.mapper.SlAttractUserMapper;
import com.cn.xiaonuo.slbfinances.attractinfo.param.SlAttractUserParam;
import com.cn.xiaonuo.slbfinances.attractinfo.service.SlAttractUserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * 招商列表
 *
 * @author 卢阳阳
 * @date 2021-03-08 14:55:13
 */
@Service
public class SlAttractUserServiceImpl extends ServiceImpl<SlAttractUserMapper, SlAttractUser> implements SlAttractUserService {

    @Resource
    private SlAttractUserMapper slAttractUserMapper;

    @Override
    @DataSource(name = "jyb_soole_data_hn")
    public PageResult<SlAttractUser> page(SlAttractUserParam slAttractUserParam) {
        if(slAttractUserParam.getShopId()==null){
            throw new ServiceException(SlAttractUserExceptionEnum.SHOP_EXIST);
        }
        QueryWrapper<SlAttractUser> queryWrapper = new QueryWrapper<>();
        if (ObjectUtil.isNotNull(slAttractUserParam)) {

            // 根据姓名 模糊查询
            if (ObjectUtil.isNotEmpty(slAttractUserParam.getRealName())) {
                queryWrapper.lambda().like(SlAttractUser::getRealName, slAttractUserParam.getRealName());
            }

            // 根据电话 模糊查询
            if (ObjectUtil.isNotEmpty(slAttractUserParam.getPhone())) {
                queryWrapper.lambda().eq(SlAttractUser::getPhone, slAttractUserParam.getPhone());
            }
            // 根据商家id
            if (ObjectUtil.isNotEmpty(slAttractUserParam.getShopId())) {
                queryWrapper.lambda().eq(SlAttractUser::getShopId, slAttractUserParam.getShopId());
            }
            // 根据电话 模糊查询
            if (ObjectUtil.isNotEmpty(slAttractUserParam.getBeginTime())&&ObjectUtil.isNotEmpty(slAttractUserParam.getBeginTime())) {
                queryWrapper.lambda().between(SlAttractUser::getCreateTime, slAttractUserParam.getBeginTime(),slAttractUserParam.getEndTime());
            }
        }
        queryWrapper.lambda().groupBy(SlAttractUser::getPhone);
        return new PageResult<>(this.page(PageFactory.defaultPage(), queryWrapper));
    }

    @Override
    @DataSource(name = "jyb_soole_data_hn")
    public List<SlAttractUser> list(SlAttractUserParam slAttractUserParam) {
        LambdaQueryWrapper<SlAttractUser> queryWrapper = new LambdaQueryWrapper<>();
        if (ObjectUtil.isNotNull(slAttractUserParam)) {
            // 根据姓名 模糊查询
            if (ObjectUtil.isNotEmpty(slAttractUserParam.getRealName())) {
                queryWrapper.like(SlAttractUser::getRealName, slAttractUserParam.getRealName());
            }

            // 根据电话查询
            if (ObjectUtil.isNotEmpty(slAttractUserParam.getPhone())) {
                queryWrapper.eq(SlAttractUser::getPhone, slAttractUserParam.getPhone());
            }
            // 根据商家id
            if (ObjectUtil.isNotEmpty(slAttractUserParam.getShopId())) {
                queryWrapper.eq(SlAttractUser::getShopId, slAttractUserParam.getShopId());
            }
            // 根据时间查询
            if (ObjectUtil.isNotEmpty(slAttractUserParam.getBeginTime())&&ObjectUtil.isNotEmpty(slAttractUserParam.getBeginTime())) {
                queryWrapper.between(SlAttractUser::getCreateTime, slAttractUserParam.getBeginTime(),slAttractUserParam.getEndTime());
            }
        }
        queryWrapper.groupBy(SlAttractUser::getPhone);
        return this.list(queryWrapper);
    }

    @Override
    @DataSource(name = "jyb_soole_data_hn")
    public void add(SlAttractUserParam SlAttractUserParam) {
        SlAttractUser SlAttractUser = new SlAttractUser();
        BeanUtil.copyProperties(SlAttractUserParam, SlAttractUser);
        this.save(SlAttractUser);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    @DataSource(name = "jyb_soole_data_hn")
    public void delete(SlAttractUserParam SlAttractUserParam) {
        this.removeById(SlAttractUserParam.getId());
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    @DataSource(name = "jyb_soole_data_hn")
    public void edit(SlAttractUserParam SlAttractUserParam) {
        SlAttractUser SlAttractUser = this.querySlAttractUser(SlAttractUserParam);
        BeanUtil.copyProperties(SlAttractUserParam, SlAttractUser);
        this.updateById(SlAttractUser);
    }

    @Override
    @DataSource(name = "jyb_soole_data_hn")
    public SlAttractUser detail(SlAttractUserParam slAttractUserParam) {
        return this.querySlAttractUser(slAttractUserParam);
    }

    /**
     * 获取SLB店铺
     *
     * @author 卢阳阳
     * @date 2021-03-08 14:55:13
     */
    @DataSource(name = "jyb_soole_data_hn")
    private SlAttractUser querySlAttractUser(SlAttractUserParam slAttractUserParam) {
        SlAttractUser slAttractUser = this.getById(slAttractUserParam.getId());
        if (ObjectUtil.isNull(slAttractUser)) {
            throw new ServiceException(SlAttractUserExceptionEnum.NOT_EXIST);
        }
        return slAttractUser;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    @DataSource(name = "jyb_soole_data_hn")
    public BusinessMessage batchOk(SlAttractUserParam slAttractUserParam){
        BusinessMessage<Object> message = new BusinessMessage<>();
        try {
            if(null==slAttractUserParam.getOperateId()){
                message.setSuccess(false);
                message.setCode("-1");
                message.setMessage("登录用户不能为空");
                return message;
            }
            ArrayList<Integer> ids=slAttractUserParam.getIds();
            if(ids==null){
                message.setSuccess(false);
                message.setCode("-1");
                message.setMessage("请选择确认项");
                return message;
            }
            for (Integer id : ids) {
                SlAttractUser entity = new SlAttractUser();
                entity.setId(id);
                entity.setOperateId(slAttractUserParam.getOperateId());
                entity.setOperateName(slAttractUserParam.getOperateName());
                entity.setUpdateTime(DateUtils.formatDate(new Date(),"yyyy-MM-dd HH:mm:ss"));
                entity.setOperateStatus(2);
                this.updateById(entity);
            }
            message.setMessage("批量确认成功！");
            message.setSuccess(true);
            return message;
        } catch (Exception e) {
            e.printStackTrace();
            message.setMessage("批量确认失败！");
            message.setSuccess(false);
            return message;
        }
    }

    /**
     * 导出
     *
     * @param slAttractUserParam
     * @author luangyang
     * @date 2022-03-08 16:08
     */

    @Override
    @DataSource(name = "jyb_soole_data_hn")
    public void export(HttpServletResponse response, SlAttractUserParam slAttractUserParam) throws Exception {
        if(slAttractUserParam.getShopId()==null){
            throw new ServiceException(SlAttractUserExceptionEnum.SHOP_EXIST);
        }
        List<SlAttractUser> list = this.list(slAttractUserParam);
        try {
            List<List<Object>> rows = new ArrayList();
            ExcelData data = new ExcelData();
            data.setName("用户信息导出");//sheet表名称
            //表头
            List<String> titles = new ArrayList();
            titles.add("时间");
            titles.add("姓名");
            titles.add("联系电话");
            titles.add("状态");
            data.setTitles(titles);
            for (SlAttractUser slAttractUser : list) {
                List<Object> row1 = new ArrayList();
                row1.add(slAttractUser.getCreateTime());
                row1.add(slAttractUser.getRealName());
                row1.add(slAttractUser.getPhone());
                //下单方式1用户下单2代客下单
                if(slAttractUser.getOperateStatus()!=null){
                    if ("1".equals(slAttractUser.getOperateStatus().toString())) {
                        row1.add("未联系");
                    } else {
                        row1.add("已联系");
                    }
                }
                rows.add(row1);
            }
            data.setRows(rows);
            ExportExcelUtils.exportExcel(response, "招商联系信息导出.xlsx", data);

        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}

